package com.ph.shopping.facade.pay.service.impl;

import java.util.Date;

import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;

import com.alibaba.dubbo.config.annotation.Service;
import com.ph.shopping.common.core.base.BaseService;
import com.ph.shopping.common.core.customenum.ResultEnum;
import com.ph.shopping.common.core.customenum.TransCodeEnum;
import com.ph.shopping.common.core.exception.BizException;
import com.ph.shopping.common.util.result.Result;
import com.ph.shopping.common.util.transform.MoneyTransUtil;
import com.ph.shopping.facade.mapper.AgentChargeRecordMapper;
import com.ph.shopping.facade.mapper.AgentOrderMapper;
import com.ph.shopping.facade.mapper.MemberChargeRecordMapper;
import com.ph.shopping.facade.mapper.MemberOrderMapper;
import com.ph.shopping.facade.mapper.MerchantChargeRecordMapper;
import com.ph.shopping.facade.mapper.MerchantOrderMapper;
import com.ph.shopping.facade.mapper.SupplierChargeRecordMapper;
import com.ph.shopping.facade.mapper.UserBalanceMapper;
import com.ph.shopping.facade.mapper.UserBalanceRecordMapper;
import com.ph.shopping.facade.merchant.entity.UserBalance;
import com.ph.shopping.facade.merchant.entity.UserBalanceRecord;
import com.ph.shopping.facade.pay.entity.AgentChargeRecord;
import com.ph.shopping.facade.pay.entity.MemberChargeRecord;
import com.ph.shopping.facade.pay.entity.MerchantChargeRecord;
import com.ph.shopping.facade.pay.entity.SupplierChargeRecord;
import com.ph.shopping.facade.pay.enums.ChargeTypeEnum;
import com.ph.shopping.facade.pay.enums.PayStatusEnum;
import com.ph.shopping.facade.pay.service.IPayService;
import com.ph.shopping.facade.pay.utils.PayecoUtils;
import com.ph.shopping.facade.pay.vo.PayecoOrderVo;

import cm.ph.shopping.facade.order.entity.PhAgentOrder;
import cm.ph.shopping.facade.order.entity.PhMerchantOrder;
import cm.ph.shopping.facade.order.entity.PhOrderOnline;

/**
 * 
 * @项目：phshopping-service-pay
 *
 * @描述：支付、提现接口
 *
 * @作者： liuy
 *
 * @创建时间：2017年3月23日
 *
 * @Copyright @2017 by liuy
 */
@Component
@Service(version="1.0.0")
public class PayServiceImpl extends BaseService  implements IPayService{

	@Autowired
	private AgentChargeRecordMapper algentChargeRecordMapper;

	@Autowired
	private MerchantChargeRecordMapper merchantChargeRecord;
	
	@Autowired
	private SupplierChargeRecordMapper supplierChargeRecordMapper;
	
	@Autowired
	private MerchantOrderMapper merchantOrderMapper;
	
	@Autowired
	private AgentOrderMapper agentOrderMapper;
	
	@Autowired
	private MemberOrderMapper memberOrderMapper;
	
	@Autowired
	private UserBalanceMapper userBalanceMapper;
	
	@Autowired
	private UserBalanceRecordMapper userBalanceRecordMapper;
	
	@Autowired
	private MemberChargeRecordMapper memberChargeRecordMapper;

	@Transactional(rollbackFor=Exception.class)
	@Override
	public Result checkAndUpdateOrderStatus(String bizCode, String orderNum,int payStatus,String responseCode)throws BizException{
		Result result=null;
		switch (bizCode) {
		case "CZ01":
			//商户充值订单更新
			MerchantChargeRecord mcr=merchantChargeRecord.getMerchantChargeRecordByRecordNo(orderNum);
			mcr.setChargeStatus((byte)payStatus);
			mcr.setUpdateTime(new Date());
			mcr.setOperater(mcr.getMerchantId());
			mcr.setResponseCode(responseCode);
			merchantChargeRecord.updateByPrimaryKeySelective(mcr);
				
			if (payStatus==PayStatusEnum.PAY_SUCCESS.getCode()) {
				//更新商户余额
				UserBalance ub=userBalanceMapper.getUserBalance(mcr.getMerchantId());
				ub.setBalance(ub.getBalance()+mcr.getScore());;
				userBalanceMapper.updateByPrimaryKeySelective(ub);
				
				//新增余额消费记录
				UserBalanceRecord ur=new UserBalanceRecord();
				ur.setCreateTime(new Date());
				ur.setMoney(mcr.getScore());
				ur.setUserId(mcr.getMerchantId());
				ur.setOrderNo(orderNum);
				ur.setTransCode(TransCodeEnum.MERCHANT_CHARGE_ORDER.getCode());
				userBalanceRecordMapper.insert(ur);
				result=getResult(ResultEnum.SUCCESS);
				return result;
			}
			break;
		case "CZ02":
			//代理商充值订单更新
			AgentChargeRecord sac=algentChargeRecordMapper.getAgentChargeRecordByRecordNo(orderNum);
			sac.setChargeStatus((byte)payStatus);
			sac.setUpdateTime(new Date());
			sac.setResponseCode(responseCode);
			sac.setOperater(sac.getAgentId());
			algentChargeRecordMapper.updateByPrimaryKeySelective(sac);
				
			if (payStatus==PayStatusEnum.PAY_SUCCESS.getCode()) {
				//更新商户余额
				UserBalance ub=userBalanceMapper.getUserBalance(sac.getAgentId());
				ub.setBalance(ub.getBalance()+sac.getScore());;
				userBalanceMapper.updateByPrimaryKeySelective(ub);
				
				//新增余额消费记录
				UserBalanceRecord ur=new UserBalanceRecord();
				ur.setCreateTime(new Date());
				ur.setMoney(sac.getScore());
				ur.setUserId(sac.getAgentId());
				ur.setOrderNo(orderNum);
				ur.setTransCode(TransCodeEnum.AGENT_CHARGE_ORDER.getCode());
				userBalanceRecordMapper.insert(ur);
				result=getResult(ResultEnum.SUCCESS);
				return result;
			}
			break;
		case "CZ03":
			//供应商充值订单更新
			SupplierChargeRecord sc=supplierChargeRecordMapper.getSupplierChargeRecordByRecordNo(orderNum);
			sc.setChargeStatus((byte)payStatus);
			sc.setResponseCode(responseCode);
			sc.setUpdateTime(new Date());
			sc.setOperater(sc.getSupplierId());
			supplierChargeRecordMapper.updateByPrimaryKeySelective(sc);
				
			if (payStatus==PayStatusEnum.PAY_SUCCESS.getCode()) {
				//更新商户余额
				UserBalance ub=userBalanceMapper.getUserBalance(sc.getSupplierId());
				ub.setBalance(ub.getBalance()+sc.getScore());;
				userBalanceMapper.updateByPrimaryKeySelective(ub);
				
				//新增余额消费记录
				UserBalanceRecord ur=new UserBalanceRecord();
				ur.setCreateTime(new Date());
				ur.setMoney(sc.getScore());
				ur.setUserId(sc.getSupplierId());
				ur.setOrderNo(orderNum);
				ur.setTransCode(TransCodeEnum.SUPPLIER_CHARGE_ORDER.getCode());
				userBalanceRecordMapper.insert(ur);
				result=getResult(ResultEnum.SUCCESS);
				return result;
			}
			
			break;
		case "JH01":
			//商户进货
			PhMerchantOrder pt=merchantOrderMapper.getMerchantOrderByOrderNum(orderNum);
			//订单状态为待付款状态才会进行订单更新
			if (payStatus==PayStatusEnum.PAY_SUCCESS.getCode()) {
				//支付成功,更新订单为待发货状态
				pt.setStatus(2);
				pt.setUpdateTime(new Date());
				merchantOrderMapper.updateByPrimaryKeySelective(pt);
			}	
			
			//新增商户支付记录
			MerchantChargeRecord mtr=new MerchantChargeRecord();
			mtr.setOrderNum(pt.getOrderNo());
			mtr.setScore(MoneyTransUtil.transMultiToBig1(pt.getTotalCost()));
			mtr.setMd5Str(pt.getMd5());
			mtr.setMerchantId(pt.getMerchantId());
			mtr.setResponseCode(responseCode);
			mtr.setChargeStatus((byte)payStatus);
			mtr.setChargeType((byte)ChargeTypeEnum.PRODUCT_PAY.getCode());
			mtr.setCreateTime(new Date());
			mtr.setUpdateTime(new Date());
			mtr.setCreater(pt.getMerchantId());
			merchantChargeRecord.insert(mtr);
			
			break;
		case "JH02":
			//代理商进货
			PhAgentOrder pa=agentOrderMapper.getAgentOrderByOrderNum(orderNum);
			if (payStatus==PayStatusEnum.PAY_SUCCESS.getCode()) {
				//支付成功,更新订单为待发货状态
				pa.setStatus(2);
				pa.setUpdateTime(new Date());
				agentOrderMapper.updateByPrimaryKeySelective(pa);
			}
			
			//新增代理商支付记录
			AgentChargeRecord scr=new AgentChargeRecord();
			scr.setOrderNum(pa.getOrderNo());
			scr.setScore(MoneyTransUtil.transMultiToBig1(pa.getTotalCost()));
			scr.setMd5Str(pa.getMd5());
			scr.setResponseCode(responseCode);
			scr.setAgentId(pa.getAgentId());
			scr.setChargeStatus((byte)payStatus);
			scr.setChargeType((byte)ChargeTypeEnum.PRODUCT_PAY.getCode());
			scr.setCreateTime(new Date());
			scr.setUpdateTime(new Date());
			scr.setCreater(pa.getAgentId());
			algentChargeRecordMapper.insert(scr);
			
			break;
		case "MO01":
			//会员订单更新
			PhOrderOnline po=memberOrderMapper.getMemberOrderByOrderNum(orderNum);
			if (payStatus==PayStatusEnum.PAY_SUCCESS.getCode()) {
					//支付成功,更新订单为待发货状态
					po.setStatus(2);
					po.setUpdateTime(new Date());
					po.setPayDate(new Date());
					memberOrderMapper.updateByPrimaryKeySelective(po);
			}
			//新增会员支付记录
			MemberChargeRecord mr=new MemberChargeRecord();
			mr.setOrderNum(po.getOrderNo());
			mr.setScore(po.getCost());
			mr.setMd5Str(po.getMd5());
			mr.setResponseCode(responseCode);
			mr.setMemberId(po.getMemberId());
			mr.setChargeStatus(payStatus);
			mr.setChargeType(ChargeTypeEnum.ONLINE_ORDER_PAY.getCode());
			mr.setCreater(po.getMemberId());
			mr.setCreateTime(new Date());
			mr.setUpdateTime(new Date());
			memberChargeRecordMapper.insert(mr);
			
			break;
		}
		return getResult(ResultEnum.SUCCESS);
	}

	@Override
	public PayecoOrderVo getOrderInfoByNum(String orderNum,String bizCode) {
		PayecoOrderVo pv=null;
		switch (bizCode) {
		case "CZ01":
			//商户充值
			MerchantChargeRecord mr=merchantChargeRecord.getMerchantChargeRecordByRecordNo(orderNum);
			pv=new PayecoOrderVo();
			BeanUtils.copyProperties(mr, pv);
			break;
		case "CZ02":
			//代理充值
			AgentChargeRecord ar=algentChargeRecordMapper.getAgentChargeRecordByRecordNo(orderNum);
			pv=new PayecoOrderVo();
			BeanUtils.copyProperties(ar, pv);
			break;
		case "CZ03":
			//供应商充值
			SupplierChargeRecord sr=supplierChargeRecordMapper.getSupplierChargeRecordByRecordNo(orderNum);
			pv=new PayecoOrderVo();
			BeanUtils.copyProperties(sr, pv);
			break;
		case "JH01":
			//商户进货
			PhMerchantOrder pt=merchantOrderMapper.getMerchantOrderByOrderNum(orderNum);
			pv=new PayecoOrderVo();
			pv.setOrderNum(pt.getOrderNo());
			pv.setScore(MoneyTransUtil.transMultiToBig1(pt.getTotalCost()));
			pv.setMd5Str(pt.getMd5());
			break;
		case "JH02":
			//代理商进货
			PhAgentOrder pa=agentOrderMapper.getAgentOrderByOrderNum(orderNum);
			pv=new PayecoOrderVo();
			pv.setOrderNum(pa.getOrderNo());
			pv.setScore(MoneyTransUtil.transMultiToBig1(pa.getTotalCost()));
			pv.setMd5Str(pa.getMd5());
			break;
		case "MO01":
			//会员订单
			PhOrderOnline po=memberOrderMapper.getMemberOrderByOrderNum(orderNum);
			pv=new PayecoOrderVo();
			pv.setOrderNum(po.getOrderNo());
			pv.setScore(po.getPayMoney());
//			pv.setMd5Str(pa.getMd5());
			break;
		}
		return pv;
	}

	@Override
	public void updateOrderMD5Sign(String orderNum, String mac)throws BizException {
		String bizCode=PayecoUtils.getOrderBizCode(orderNum);
		switch (bizCode) {
		case "CZ01":
			//商户充值
			MerchantChargeRecord mr=merchantChargeRecord.getMerchantChargeRecordByRecordNo(orderNum);
			if (!StringUtils.isEmpty(mr)) {
				mr.setMd5Str(mac);//更新mac值到订单表,回调时调用
				merchantChargeRecord.updateByPrimaryKeySelective(mr);
			}
			
			break;
		case "CZ02":
			//代理充值
			AgentChargeRecord ar=algentChargeRecordMapper.getAgentChargeRecordByRecordNo(orderNum);
			if (!StringUtils.isEmpty(ar)) {
				ar.setMd5Str(mac);//更新mac值到订单表,回调时调用
				algentChargeRecordMapper.updateByPrimaryKeySelective(ar);
			}
			break;
		case "CZ03":
			//供应商充值
			SupplierChargeRecord sr=supplierChargeRecordMapper.getSupplierChargeRecordByRecordNo(orderNum);
			if (!StringUtils.isEmpty(sr)) {
				sr.setMd5Str(mac);//更新mac值到订单表,回调时调用
				supplierChargeRecordMapper.updateByPrimaryKeySelective(sr);
			}
			break;
		case "JH01":
			//商户进货
			PhMerchantOrder pt=merchantOrderMapper.getMerchantOrderByOrderNum(orderNum);
			if (!StringUtils.isEmpty(pt)) {
				pt.setMd5(mac);//更新mac值到订单表,回调时调用
				merchantOrderMapper.updateByPrimaryKeySelective(pt);
			}
			break;
		case "JH02":
			//代理商进货
			PhAgentOrder pa=agentOrderMapper.getAgentOrderByOrderNum(orderNum);
			if (!StringUtils.isEmpty(pa)) {
				pa.setMd5(mac);//更新mac值到订单表,回调时调用
				agentOrderMapper.updateByPrimaryKeySelective(pa);
			}
			break;
		case "MO01":
			//会员订单
			PhOrderOnline po=memberOrderMapper.getMemberOrderByOrderNum(orderNum);
			if (!StringUtils.isEmpty(po)) {
//				po.setMd5(mac);//更新mac值到订单表,回调时调用
				memberOrderMapper.updateByPrimaryKeySelective(po);
			}
			break;
		}
	}

}
